//
//  TIMManagerGroupVC.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/9.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMManagerGroupVC.h"
#import "UIView+Extension.h"
#import "TIMDemoMacro.h"
#import "TIMGroupAdministratorVC.h"
#import "ProgressHUD.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMManagerGroupVC ()
@property (nonatomic, strong) UISwitch *switchBtn;
@property (nonatomic, strong) TIMGroupInfo *groupInfo;

@property (nonatomic,assign) BOOL groupOwnerIsSelf;//!<

@end

@implementation TIMManagerGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = DefaultBackGroundColor;
    
    TIMUserInfo *currentUser = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
     self.groupOwnerIsSelf = [self.groupInfo.groupOwner isEqualToString:currentUser.userID] ? YES : NO;
    
    [self setContentView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setContentView
{
    // 全员禁言
    UIView *gagView = [[UIView alloc] initWithFrame:CGRectMake(0, 94, Screen_Width, 44)];
    gagView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:gagView];
    
    UILabel *gagTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    gagTitleLB.text = @"全员禁言";
    gagTitleLB.font = TPSFontSize(AUTO_WIDTH(16));
    [gagView addSubview:gagTitleLB];
    
    UISwitch *gagSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(gagView.width - AUTO_WIDTH(51), 0, AUTO_WIDTH(51), AUTO_WIDTH(31))];
    gagSwitch.onTintColor = UIColor.blueColor;
    gagSwitch.centerY = gagView.height/2;
    gagSwitch.on = self.groupInfo.stop_talk;
    [gagSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    self.switchBtn = gagSwitch;
    [gagView addSubview:gagSwitch];
    
    UILabel *gagDetailLB = [[UILabel alloc] initWithFrame:CGRectMake(AUTO_WIDTH(15), CGRectGetMaxY(gagView.frame) + AUTO_WIDTH(5), Screen_Width - AUTO_WIDTH(25), AUTO_WIDTH(15))];
    gagDetailLB.text = @"开启禁言后，只有群主和管理员可以发送消息";
    gagDetailLB.textColor = RGBColor(117, 117, 117);
    gagDetailLB.font = TPSFontSize(AUTO_WIDTH(12));
    [self.view addSubview:gagDetailLB];
    
    CGFloat offY = CGRectGetMaxY(gagDetailLB.frame);
    // 设置管理员
    if (self.groupOwnerIsSelf) {
        UIView *settingView = [[UIView alloc] initWithFrame:CGRectMake(0, offY + AUTO_WIDTH(10), Screen_Width, AUTO_WIDTH(44))];
        settingView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:settingView];
        
        UILabel *settingTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(AUTO_WIDTH(15), 0, AUTO_WIDTH(100), AUTO_WIDTH(44))];
        settingTitleLB.text = @"设置管理员";
        settingTitleLB.font = TPSFontSize(AUTO_WIDTH(16));
        [settingView addSubview:settingTitleLB];
        
        UIImageView *goImgView = [[UIImageView alloc]initWithImage:kLOADIMAGE(@"mine_edit_go")];
        goImgView.x = Screen_Width - AUTO_WIDTH(15) - goImgView.image.size.width;
        goImgView.centerY = AUTO_WIDTH(22);
        [settingView addSubview:goImgView];
        
        UIButton *settingBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, settingView.width, settingView.height)];
        settingBtn.backgroundColor = [UIColor clearColor];
        [settingBtn addTarget:self action:@selector(settingBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [settingView addSubview:settingBtn];
        
        offY += settingView.height + AUTO_WIDTH(10);
        
        // 转让群 attorn
        UIView *attornView = [[UIView alloc] initWithFrame:CGRectMake(0, offY + AUTO_WIDTH(10), Screen_Width, AUTO_WIDTH(44))];
        attornView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:attornView];
        
        UILabel *attornTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(AUTO_WIDTH(15), 0, AUTO_WIDTH(120), AUTO_WIDTH(44))];
        attornTitleLB.text = @"群主管理权转让";
        attornTitleLB.font = TPSFontSize(AUTO_WIDTH(16));
        [attornView addSubview:attornTitleLB];
        
        UIImageView *attornImgView = [[UIImageView alloc]initWithImage:kLOADIMAGE(@"mine_edit_go")];
        attornImgView.x = Screen_Width - AUTO_WIDTH(15) - attornImgView.image.size.width;
        attornImgView.centerY = AUTO_WIDTH(22);
        [attornView addSubview:attornImgView];
        
        UIButton *attornBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, AUTO_WIDTH(44))];
        attornBtn.backgroundColor = [UIColor clearColor];
        [attornBtn addTarget:self action:@selector(attornBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [attornView addSubview:attornBtn];
        
        offY += attornView.height + AUTO_WIDTH(10);
        
        // 邀请待确认
        UIView *inviteView = [[UIView alloc] initWithFrame:CGRectMake(0,  offY + AUTO_WIDTH(10), Screen_Width, 44)];
        inviteView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:inviteView];
        
        UILabel *gagTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
        gagTitleLB.text = @"群聊邀请确认";
        gagTitleLB.font = TPSFontSize(AUTO_WIDTH(16));
        [inviteView addSubview:gagTitleLB];
        
        UISwitch *inviteSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(gagView.width - AUTO_WIDTH(51), 0, AUTO_WIDTH(51), AUTO_WIDTH(31))];
        inviteSwitch.onTintColor = UIColor.blueColor;
        inviteSwitch.centerY = gagView.height/2;
        inviteSwitch.on = self.groupInfo.offerIsNeedAck.boolValue;
        [inviteSwitch addTarget:self action:@selector(switchInviteAction:) forControlEvents:UIControlEventValueChanged];
        self.switchBtn = inviteSwitch;
        [inviteView addSubview:inviteSwitch];

        
        UILabel *inviteDetailLB = [[UILabel alloc] initWithFrame:CGRectMake(AUTO_WIDTH(15), CGRectGetMaxY(inviteView.frame) + AUTO_WIDTH(5), Screen_Width - AUTO_WIDTH(25), AUTO_WIDTH(15))];
        inviteDetailLB.text = @"启用后，群成员需要群主确认后才能进群";
        inviteDetailLB.textColor = RGBColor(117, 117, 117);
        inviteDetailLB.font = TPSFontSize(AUTO_WIDTH(12));
        [self.view addSubview:inviteDetailLB];
        
        
         offY += inviteView.height + AUTO_WIDTH(30);
        
        // 删除群成员通知
        UIView *deleteNotifyView = [[UIView alloc] initWithFrame:CGRectMake(0,  offY + AUTO_WIDTH(10), Screen_Width, 44)];
        deleteNotifyView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:deleteNotifyView];
        
        UILabel *deleteTitleLB = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 140, 44)];
        deleteTitleLB.text = @"删除群成员通知";
        deleteTitleLB.font = TPSFontSize(AUTO_WIDTH(16));
        [deleteNotifyView addSubview:deleteTitleLB];
        
        UISwitch *deleteSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(gagView.width - AUTO_WIDTH(51), 0, AUTO_WIDTH(51), AUTO_WIDTH(31))];
        deleteSwitch.onTintColor = UIColor.blueColor;
        deleteSwitch.centerY = gagView.height/2;
        deleteSwitch.on = self.groupInfo.kickOffIsDisplay.boolValue;
        [deleteSwitch addTarget:self action:@selector(switchDeleteNotifyAction:) forControlEvents:UIControlEventValueChanged];
        self.switchBtn = deleteSwitch;
        [deleteNotifyView addSubview:deleteSwitch];
        
        UILabel *gagDetailLB = [[UILabel alloc] initWithFrame:CGRectMake(AUTO_WIDTH(15), CGRectGetMaxY(deleteNotifyView.frame) + AUTO_WIDTH(5), Screen_Width - AUTO_WIDTH(25), AUTO_WIDTH(15))];
        gagDetailLB.text = @"启用后，删除群成员时所有人都能看到";
        gagDetailLB.textColor = RGBColor(117, 117, 117);
        gagDetailLB.font = TPSFontSize(AUTO_WIDTH(12));
        [self.view addSubview:gagDetailLB];
    }
}

#pragma mark - Private method
// 群禁言
-(void)switchAction:(id)sender {
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        [TIMApi operateGroupWithGroupId:self.groupID operateUserId:nil MsgOperation:TIMOperateGroupTypeStopTalkGroup groupMembers:nil GroupName:nil completion:^(NSError *error) {
            if (!error) {
                 NSLog(@"-------------xiadeli-------------禁言成功ok");
            } else {
                [ProgressHUD showHudTipStr:@"禁言失败"];
            }
        }];
    } else {
        [TIMApi operateGroupWithGroupId:self.groupID operateUserId:nil MsgOperation:TIMOperateGroupTypeOpenTalkGroup groupMembers:nil GroupName:nil completion:^(NSError *error) {
            if (!error) {
                  NSLog(@"-------------xiadeli-------------开言成功ok");
            } else {
                 [ProgressHUD showHudTipStr:@"开言失败"];
            }
        }];
    }
}

- (void)switchInviteAction:(id)sender {
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        [TIMApi inviteOperationWithGroup:self.groupID inviteRemarks:nil inviteAuditAck:nil MsgOperation:TIMOperateGroupTypeGrpOfferAuditOpen groupMembers:nil completion:^(NSError *error) {
            if (!error) {
                NSLog(@"-------------xiadeli------------邀请开头打开ok");
            } else {
                [ProgressHUD showHudTipStr:@"打开开关失败"];
            }
        }];
    } else {
        [TIMApi inviteOperationWithGroup:self.groupID inviteRemarks:nil inviteAuditAck:nil MsgOperation:TIMOperateGroupTypeGrpOfferAuditStop groupMembers:nil completion:^(NSError *error) {
            if (!error) {
                NSLog(@"-------------xiadeli------------邀请开头打开ok");
            } else {
                [ProgressHUD showHudTipStr:@"打开开关失败"];
            }
        }];
    }
}

- (void)switchDeleteNotifyAction:(id)sender {
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    if (isButtonOn) {
        [TIMApi operateGroupWithGroupId:self.groupID operateUserId:nil MsgOperation:TIMOperateGroupTypeGrpKickOffMsgOpen groupMembers:nil GroupName:nil completion:^(NSError *error) {
            if (!error) {
                NSLog(@"-------------xiadeli------------踢人开着通知打开");
            } else {
                [ProgressHUD showHudTipStr:@"打开开关失败"];
            }
        }];
    } else {
        [TIMApi operateGroupWithGroupId:self.groupID operateUserId:nil MsgOperation:TIMOperateGroupTypeGrpKickOffMsgStop groupMembers:nil GroupName:nil completion:^(NSError *error) {
            if (!error) {
                NSLog(@"-------------xiadeli------------踢人开着通知关闭");
            } else {
                [ProgressHUD showHudTipStr:@"打开开关失败"];
            }
        }];
    }
}

// 设置群管理员
- (void)settingBtnClicked
{
    TIMGroupAdministratorVC *vc = [TIMGroupAdministratorVC new];
    vc.groupId = self.groupID;
    vc.managerGroup = 1;
    [self.navigationController pushViewController:vc animated:YES];
}

// 转让群
- (void)attornBtnClicked
{
    if (self.groupOwnerIsSelf) {
        TIMGroupAdministratorVC *vc = [TIMGroupAdministratorVC new];
        vc.groupId = self.groupID;
        vc.managerGroup = 0;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        [ProgressHUD showHudTipStr:@"你不是群主"];
    }
}

#pragma mark - Get
- (TIMGroupInfo *)groupInfo {
    if (!_groupInfo) {
        _groupInfo = [[TIMStorageManager sharedStorageManager] getGroupInfoByGroupID:self.groupID];
    }
    return _groupInfo;
}

@end
